package shuai.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sun.glass.ui.Application;

import shuai.dao.User;
import shuai.dao.UserJdbcDao;
import shuai.dao.UserNamedJdbcTemplateDao;
import shuai.dao.UserNamedSupportDao;
import shuai.dao.UserSupportDao;
import shuai.dao.UserTemplateDao;



public class Demo {
	@Test
	public void test1() throws SQLException{
		BasicDataSource ds=new BasicDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3307/ssm");
		ds.setUsername("root");
		ds.setPassword("1234");
		ds.setInitialSize(50);
		ds.setMaxActive(40);
		ds.setMaxIdle(30);
		ds.setMinIdle(1);
		ds.setMaxWait(30000);
		
		Connection connection=(Connection) ds.getConnection();
		System.out.println("链接数量"+ds.getNumActive());
		System.out.println("闲置数量"+ds.getNumIdle());
		ds.close();
	}
	@Test
	public void test2(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserJdbcDao userJdbcDao=ac.getBean("userJdbcDao",UserJdbcDao.class);
		List<User> list=userJdbcDao.searchAll();
		for(User u:list){
			System.out.println(u.getUsername()+":"+u.getAge());
		}
	}
	
	@Test
	public void test3(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserTemplateDao dao=ac.getBean("userTemplateDao",UserTemplateDao.class);
		User user=new User("shuai","shuai",25);
		dao.save(user);
	}
	
	@Test
	public void test4(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserTemplateDao dao=ac.getBean("userTemplateDao",UserTemplateDao.class);
		List<User> list=dao.search();
		for(User user:list){
			System.out.println(user.getUsername()+user.getAge());
		}
	}
	
	@Test
	public void test5(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserTemplateDao dao=ac.getBean("userTemplateDao",UserTemplateDao.class);
		User user=dao.findById(1);
		System.out.println(user.getUsername());
	}
	
	@Test
	public void test6(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserTemplateDao dao=ac.getBean("userTemplateDao",UserTemplateDao.class);
		User user=new User();
		user.setUsername("k");
		user.setAge(10);
		List<User> list=dao.search(user);
		for(User u:list){
			System.out.println(u.getUsername());
		}
	}
	
	@Test
	public void test7(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserNamedJdbcTemplateDao dao=ac.getBean("userNamedTemplateDao",UserNamedJdbcTemplateDao.class);
		User user=new User("kongshuai","kongshuai",111);
		dao.save(user);
	}
	
	@Test
	public void test8(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserSupportDao dao=ac.getBean("userSupportDao",UserSupportDao.class);
		User user=new User("ks","ks",110);
		dao.save(user);
	}
	
	@Test
	public void test9(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserNamedSupportDao dao=ac.getBean("userNamedSupportDao",UserNamedSupportDao.class);
		User user=new User("shuaishuai","shuaishuai",100);
		dao.save(user);
	}
}
